package com.teamb6.shop.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.teamb6.shop.domain.SearchResponse;
import com.teamb6.shop.domain.User;
import com.teamb6.shop.domain.UserExample;
import com.teamb6.shop.mapper.UserMapper;
import com.teamb6.shop.service.MailService;
import com.teamb6.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private MailService mailService;

    /**
     * 检查用户名是否已经存在
     * @param userExample
     * @return
     */
    @Override
    public List<User> selectByExample(UserExample userExample) {
        return userMapper.selectByExample(userExample);
    }

    /**
     * 用户注册，插入一条数据，此时状态为0还未激活
     * @param user
     */
    @Override
    public boolean insertUser(User user) {
        String code = user.getCode();
        System.out.println("code:"+code);
        String subject = "来自B06Shop网站的激活邮件";
        String content = "<a href=\"http://localhost:8080/users/checkCode?code="+code+"\">激活请点击:"+code+"</a>";

        //发送邮件
        if(!mailService.sendMail(user.getEmail(),subject,content)){
            return false;
        }

        userMapper.insertSelective(user);
        return true;
    }

    /**
     * 根据激活码code查询用户，若用户存在则修改用户状态为1
     * @param code
     * @return
     */
    @Override
    public User checkCode(String code) {
        return userMapper.selectByCode(code);
    }

    /**
     * 激活账户，修改状态码
     */
    @Override
    public void updateUserStatus(User user) {
        userMapper.updateUserStatus(user);
    }

    //分页查询员工
    @Override
    public SearchResponse listUser(Integer pageNum, Integer pageSize, String keyword) {
        keyword="%"+keyword+"%";
        PageHelper.startPage(pageNum,pageSize);
        UserExample example = new UserExample();
        example.or().andUsernameLike(keyword);
        List<User> users = userMapper.selectByExample(example);
        return new SearchResponse(((Page)users).getTotal(),users);
    }
    //删除一行
    @Override
    public int deleteUserByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    //批量删除

    @Override
    public int multiDelUsers(String ids) {
        UserExample example = new UserExample();
        List<String> list = Arrays.asList(ids.split(","));
        List<Integer> ilist = list.stream().map(Integer::parseInt).collect(Collectors.toList());
        example.or().andUseridIn(ilist);
        return userMapper.deleteByExample(example);
    }

    //得到选中用户的信息
    @Override
    public User findUserByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    //保存编辑后的用户信息
    @Override
    public int editUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public String find(String username) {
        return userMapper.find(username);
    }

}
